package com.tito.common.api;

import com.tito.common.api.constant.BaseStatusCode;
import com.tito.common.api.constant.StatusCode;

import java.util.HashMap;
import java.util.Map;

public class ResultBean<E> extends BaseResult
{
    private E data = null; // 返回数据

    public ResultBean()
    {
        this.setOk(true);
    }

    public ResultBean(E result)
    {
        this.setOk(true);
        this.setData(result);
    }

    public ResultBean(StatusCode code, String msg)
    {
        if (BaseStatusCode.SUCCESS.getCode().equals(code.getCode()))
        {
            this.setOk(true);
        }
        else
        {
            this.setOk(false);
        }
        this.setStatusCode(code);
        this.setMsg(msg);
    }

    public E getData()
    {
        return data;
    }

    public void setData(E data)
    {
        this.data = data;
    }

    public void addMapParam(String key, Object val)
    {
        if (data == null)
        {
            Map map = new HashMap();
            this.data = (E) map;
        }
        if (!(this.data instanceof Map))
        {
            throw new RuntimeException("设置参数异常！当前返回结果非map对象，无法使用 addMapParam方法获取数据");
        }

        Map map = (Map) data;
        map.put(key, val);
    }

    public Object getMapParam(String key)
    {
        if (!(this.data instanceof Map))
        {
            throw new RuntimeException("获取参数异常！当前返回结果非map对象，无法使用 addMapParam方法获取数据");
        }

        Map map = (Map) data;
        return map.get(key);
    }
}
